﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace XNA_Game_Framework
{
    static internal class DropMenuDraw
    {
        static DropMenu parent;
        static SpriteBatch spriteBatch;
		static int numberOfSlices;

        static internal int LineHeight
		{
			get 
			{
				SpriteFont font = parent.Font;
				Vector2 lineSize = font.MeasureString("example");
				return (int)lineSize.Y;
			}
		}

		static internal int LineSeparation
		{
			get
			{
				return parent.selectionBox.Height - LineHeight;
			}
		}

		static internal int HalfLineSeparation
		{
			get
			{
				return LineSeparation / 2;
			}
		}

		static internal int SelectionBoxHeight
		{
			get {return parent.selectionBox.Height;}
		}

		static internal int TopBorderHeight
		{
			get { return parent.topBorder.Height; }
		}

        static public void Start(DropMenu dropmenu, SpriteBatch spriteBatchIn)
		{
			parent = dropmenu;
            spriteBatch = spriteBatchIn;
            
            DrawBorders();
			DrawSelectionBox();
            DrawText();
		}

        static private void DrawBorders()
        {
			DrawTopBorder();
			DrawSideBorders();
			DrawBottomBorder();
        }

		static private void DrawTopBorder()
		{
			Vector2 upperLeftCorner = parent.WindowPosition;
			spriteBatch.Draw(parent.topBorder, upperLeftCorner, Color.White);
		}

		static private void DrawSideBorders()
		{
			Vector2 firstSlicePosition = new Vector2(parent.WindowPosition.X, parent.WindowPosition.Y + TopBorderHeight);
			numberOfSlices = 0;

			Vector2 currentSlicePosition = firstSlicePosition;
			for (int i = 0; i < parent.menuItems.Count; i++)
			{
				numberOfSlices++;
				spriteBatch.Draw(parent.sideBorders, currentSlicePosition, Color.White);
				currentSlicePosition.Y += SelectionBoxHeight;
			}
		}

		static private void DrawBottomBorder()
		{
			Vector2 upperLeftCornerAfterLastSlice = new Vector2(parent.WindowPosition.X, parent.WindowPosition.Y + TopBorderHeight + numberOfSlices * SelectionBoxHeight);
			spriteBatch.Draw(parent.bottomBorder, upperLeftCornerAfterLastSlice, Color.White);
		}

		static private void DrawSelectionBox()
		{
			Vector2 linePosition = new Vector2(parent.WindowPosition.X, parent.WindowPosition.Y + TopBorderHeight + parent.CurrentIndex * SelectionBoxHeight);
			spriteBatch.Draw(parent.selectionBox, linePosition, Color.White);
		}

        static private void DrawText()
        {
			Vector2 linePosition = new Vector2(parent.WindowPosition.X + HalfLineSeparation, parent.WindowPosition.Y + TopBorderHeight + HalfLineSeparation);
            for (int i = 0; i < parent.menuItems.Count(); i++)
			{
				string entry = parent.menuItems[i].Text;
				spriteBatch.DrawString(parent.Font, entry, linePosition, Color.Black);
				linePosition.Y += parent.selectionBox.Height;
			}
        }
    }
}
